<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false" title="下载数据">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="数据来源:">
              <a-select v-model:value="where.source" style="width: 300px;">
                <a-select-option :value="3">下载喷淋设备数据</a-select-option>
                <a-select-option :value="5">下载奶牛瘤胃温度</a-select-option>
                <a-select-option :value="7">下载奶牛的呼吸心跳</a-select-option>
                <a-select-option :value="9">下载环控设备的数据</a-select-option>
                <a-select-option :value="12">下载奶牛按天统计的躺卧数据</a-select-option>
                <a-select-option :value="13">下载奶牛按小时统计的躺卧数据</a-select-option>
                <a-select-option :value="15">下载所有的呼吸心跳设备的数据</a-select-option>
                <a-select-option :value="18">下载水表设备抄表的实时数据</a-select-option>
                <a-select-option :value="19">下载水表设备抄表的天级别统计数据</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="所属牧场:" name="orgId">
              <a-select
                show-search
                v-model:value="where.orgId"
                placeholder="请选择所属牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="selectRanch"
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="所属牛舍:" name="oxtId">
              <a-select
                show-search
                v-model:value="where.oxtId"
                placeholder="请选择所属牛舍"
                style="width: 200px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
                  {{ item.oxtallName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-row>
          <a-row style="margin-top: 10px;">
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button @click="reset">重置</a-button>
                <a-button @click="downloadSpray">下载</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>
  </div>
</template>

<script>
import { SysDatadownLogApi } from '@/api/system/operation/SysDatadownLogApi';
import { message } from 'ant-design-vue';
import {RanchApi} from "@/api/property/RanchApi";
import {OxtallApi} from "@/api/property/OxtallApi";
export default {
  name: 'OperationSysDatadownOp',
  components: {
  },
  async mounted() {
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
      if (_ranchList.length > 0) {
        OxtallApi.list({"orgId" : _ranchList[0].ranchId}).then(result => {
          this.oxtallList = (result ? result.data : null) || [];
        }).catch((err) => {
          console.log(err)
        });
      }
    });
  },
  data() {
    return {
      // 表格搜索条件
      where: {},
      ranchList : [],
      oxtallList : [],
    };
  },
  methods: {
    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/06/17 23:28
     */
    reset() {
      this.where.source = '';
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.where.oxtId = '';
      this.where.orgId = '';
    },
    downloadSpray () {
      if (this.where.source == null || this.where.source <= 0) {
        message.warn("请选择数据源");
        return;
      }
      if (this.where.searchBeginTime && this.where.searchEndTime) {
        let fromParam = {
          "searchBeginTime" : this.where.searchBeginTime,
          "searchEndTime" : this.where.searchEndTime,
          "oxtId" : this.where.oxtId,
          "ranchId" : this.where.orgId
        };
        let from = {
          "downName" : this.where.searchBeginTime + "~" + this.where.searchEndTime + "的数据下载",
          "downScene" : this.where.source,
          "downCondition" : JSON.stringify(fromParam)
        };
        if (this.where.source == 3) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的喷淋数据下载";
        } else if (this.where.source == 5) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的奶牛瘤胃温度数据下载";
        } else if (this.where.source == 7) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的奶牛的呼吸心跳数据下载";
        } else if (this.where.source == 9) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的环控设备的数据下载";
        } else if (this.where.source == 12) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的奶牛按天统计的躺卧数据下载";
        } else if (this.where.source == 13) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的奶牛按小时统计的躺卧数据下载";
        } else if (this.where.source == 15) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的所有的呼吸心跳设备的数据下载";
        } else if (this.where.source == 18) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的牧场或牛舍下的水表设备抄表实时数据下载";
        } else if (this.where.source == 19) {
          from.downName = this.where.searchBeginTime + "~" + this.where.searchEndTime + "的牧场或牛舍下的水表设备抄表天级别的数据下载";
        }
        let that = this;
        let result = SysDatadownLogApi.add(from);
        result
          .then(result1 => {
            message.success("添加下载任务成功，请移步到【数据下载】->【导出记录】查看")
            that.reset();
          })
          .catch(() => {
          });
      } else {
        message.warn("请选择时间条件");
      }
    },
    selectRanch(ranchId) {
      this.oxtallList = [];
      this.where.oxtId = undefined;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    },
  }
};
</script>
